package com.gin.kotlin.no_gay_alliance.adapter.post

import android.content.Context
import android.graphics.Typeface
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
import com.gin.kotlin.no_gay_alliance.R
import com.gin.kotlin.no_gay_alliance.base.AbstractBindingRecyclerViewAdapter
import com.gin.kotlin.no_gay_alliance.base.GlideModule.Companion.CROSS_FADE
import com.gin.kotlin.no_gay_alliance.base.MyHandler
import com.gin.kotlin.no_gay_alliance.base.TextViewTarget
import com.gin.kotlin.no_gay_alliance.databinding.ItemPostBbsCodeBinding
import com.gin.kotlin.no_gay_alliance.model.PostBbsCodeModel

class BbsCodeAdapter(context: Context, handler: MyHandler) :
    AbstractBindingRecyclerViewAdapter<ItemPostBbsCodeBinding, PostBbsCodeModel>(context, handler, false, PostBbsCodeModel.entries.toMutableList()) {
    override fun onBindViewHolder(holder: ViewHolder<ItemPostBbsCodeBinding>, position: Int, b: ItemPostBbsCodeBinding, dataModel: PostBbsCodeModel) {
        b.tvCodeName.text = dataModel.cn
        b.tvCodeName.setOnClickListener { dataModel.onClick(context, handler) }
        b.tvCodeName.setOnLongClickListener { dataModel.onLongClick(context, handler) }

        b.tvCodeName.setTypeface(null)
        if (dataModel.longClick) b.tvCodeName.setTypeface(b.tvCodeName.typeface, Typeface.BOLD_ITALIC)

        val size = (32 * context.resources.displayMetrics.density).toInt()
        Glide.with(context)
            .load(dataModel.icon)
            .placeholder(R.drawable.ic_quote_24)
            .transition(DrawableTransitionOptions.withCrossFade(CROSS_FADE))
            .into(TextViewTarget(b.tvCodeName, 1, size, size))
    }
}